package com.mytrip.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

public class CacheInRequest{
	private static String CacheInRequest = "CACHE_IN_REQUEST";

	public static Object getResult(Object key){
		Map<Object, Object> cacheMap = getMap();
		if (cacheMap == null) {
			return null;
		}
		return cacheMap.get(key);
	}

	public static Object setResult(Object key, Object value){
		Map<Object, Object> cacheMap = getMap();
		if (cacheMap == null) {
			return value;
		}
		return cacheMap.put(key, value);
	}

	@SuppressWarnings("unchecked")
	private static Map<Object, Object> getMap(){
		HttpServletRequest request = ServletActionContext.getRequest();
		if (request == null) {
			return null;
		}
		Map<Object, Object> cacheMap = (Map<Object, Object>) request.getAttribute(CacheInRequest);
		if (cacheMap == null) {
			cacheMap = new HashMap<Object, Object>();
			request.setAttribute(CacheInRequest, cacheMap);
		}
		return cacheMap;
	}
}
